package com.hupu.jrs.ui.gallery

import android.net.Uri
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.backends.pipeline.PipelineDraweeController
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.imagepipeline.common.ResizeOptions
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.hupu.jrs.R
import com.hupu.jrs.bean.Image
import java.io.File
import java.util.*
import javax.inject.Inject

/**
 * Created by sll on 2016/3/10.
 */
class ImageAdapter @Inject
constructor() : RecyclerView.Adapter<ImageAdapter.ViewHolder>() {

    private var images: MutableList<Image>? = ArrayList<Image>()
    private val selectedImages = ArrayList<Image>()
    private var mOnImageItemClickListener: OnImageItemClickListener? = null

    fun bind(images: MutableList<Image>?) {
        selectedImages.clear()
        if (images != null && images.size > 0) {
            this.images = images
        } else {
            this.images!!.clear()
        }
        notifyDataSetChanged()
    }

    fun select(image: Image, ivCheck: ImageView) {
        image.checked = !image.checked
        if (image.checked) {
            selectedImages.add(image)
            ivCheck.setImageResource(R.drawable.ap_gallery_checked)
        } else {
            selectedImages.remove(image)
            ivCheck.setImageResource(R.drawable.ap_gallery_normal)
        }
    }

    fun setDefaultSelected(resultList: ArrayList<String>) {
        for (path in resultList) {
            val image = getImageByPath(path)
            if (image != null) {
                selectedImages.add(image)
            }
        }
        if (selectedImages.size > 0) {
            notifyDataSetChanged()
        }
    }

    private fun getImageByPath(path: String): Image? {
        if (images != null && images!!.size > 0) {
            for (image in images!!) {
                if (image.path.equals(path)) {
                    return image
                }
            }
        }
        return null
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.item_grid_gallery, parent, false)
        return ViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val image = images!![position]
        holder.image = image
        if (image == null) {
            return
        }
        holder.ivCheck!!.visibility = View.VISIBLE
        holder.ivCheck!!.setImageResource(if (selectedImages.contains(image))
            R.drawable.ap_gallery_checked
        else
            R.drawable.ap_gallery_normal)
        val width = 100
        val height = 100
        val request = ImageRequestBuilder.newBuilderWithSource(Uri.fromFile(File(image!!.path)))
                .setResizeOptions(ResizeOptions(width, height))
                .build()
        val controller = Fresco.newDraweeControllerBuilder()
                .setOldController(holder.ivPhoto!!.controller)
                .setImageRequest(request)
                .build() as PipelineDraweeController
        holder.ivPhoto!!.controller = controller
    }

    override fun getItemCount(): Int {
        return images!!.size
    }

    fun setOnImageItemClickListener(mOnImageItemClickListener: OnImageItemClickListener) {
        this.mOnImageItemClickListener = mOnImageItemClickListener
    }

    interface OnImageItemClickListener {
        fun click(image: Image?, view: ImageView)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        @BindView(R.id.ivPhoto)
        var ivPhoto: SimpleDraweeView? = null
        @BindView(R.id.ivCheck)
        var ivCheck: ImageView? = null
        var image: Image? = null

        @OnClick(R.id.flItem)
        fun flItemClick() {
            if (mOnImageItemClickListener != null) {
                mOnImageItemClickListener!!.click(image, this!!.ivCheck!!)
            }
        }

        init {
            ButterKnife.bind(this, view)
        }
    }
}
